<?php
class CAction {
	
	public $fileType = '.php';
	
	public $tplVar   = null;
	
	/**
	 * 模版解析
	 * Enter description here ...
	 */
	public function display($filename) {
		$fileDir = SoftBase::$config['basePath'].'views/'.strtolower(SoftBase::$callAction[1]);
		$html = $fileDir.'/'.$filename.'.html';
		//判断目录是否存在
		if(! is_dir($fileDir)) {
			SoftBase::mkdir($fileDir);
		}
		if(! file_exists($html)) {
			throw new Exception('Template file does not exist :'.$html);
		}
		if ( is_array($this->tplVar) && !empty($this->tplVar) ) 
		{
			extract($this->tplVar);
		}
		if(SoftBase::$config['cache']['CCacheFile'] == true) {
			$data = CCache::fetch($html);
			$cacheFile = CCache::getcache($html);
			if(! isset($cacheFile)) {
				CCache::setcache($html,$data);@ob_start('ob_gzip');print $data;ob_end_flush();
			} else {
				print $cacheFile;
			}
		} else {
			require_once($html);
		}
	}
	
	/**
	 * 参数设置
	 * Enter description here ...
	 * @param unknown_type $key
	 * @param unknown_type $val
	 */
	public function set($key, $val) {
		
		if(empty($key) && empty($val)) { return false;}
		
		$this->tplVar[$key] = $val;
	}
	
	/**
	 * 呼叫模块调用
	 * Enter description here ...
	 */
	public function call() {
		$fileDir = SoftBase::$config['basePath'].'controllers/'.strtolower(SoftBase::$callAction[1]).'Controllers'.$this->fileType;	
		//判断目录是否存在
		if(! is_file($fileDir)) {
			throw new Exception('This controller does not exist : '.strtolower(SoftBase::$callAction[1]).'Controllers'.$this->fileType);
		}
		require_once "{$fileDir}";
		$class   = strtolower(SoftBase::$callAction[1]).'Controllers';
		$cAction = new $class();
		if(! method_exists($cAction, SoftBase::$callAction[2])) {
			
			throw new Exception('This function does not exist : '.strtolower(SoftBase::$callAction[2]));
		} 
		$function  = SoftBase::$callAction[2];
		
		$cAction->$function();
	}
	
	/**
	 * 析构函数
	 * Enter description here ...
	 */
	public function __destruct() {
		unset($this->tplVar);
	}
}